home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_glimpse.idb / usr / freeware / src / glimpse-3.0 / libtemplate / template / iafa2soif.c.z / iafa2soif.c
C/C++ Source or Header  |  1997-09-09  |  4KB  |  148 lines

  1. static char rcsid[] = "$Id: iafa2soif.c,v 1.8 1995/01/10 16:30:33 hardy Exp $";
  2. /*
  3.  *  iafa2soif - Converts IAFA templates to SOIF.
  4.  *
  5.  *  Usage: iafa2soif url local-file
  6.  *
  7.  *  Darren Hardy, hardy@cs.colorado.edu, June 1994
  8.  *
  9.  *  ----------------------------------------------------------------------
  10.  *  Copyright (c) 1994, 1995.  All rights reserved.
  11.  *  
  12.  *          Mic Bowman of Transarc Corporation.
  13.  *          Peter Danzig of the University of Southern California.
  14.  *          Darren R. Hardy of the University of Colorado at Boulder.
  15.  *          Udi Manber of the University of Arizona.
  16.  *          Michael F. Schwartz of the University of Colorado at Boulder. 
  17.  *  
  18.  *  This copyright notice applies to all code in Harvest other than
  19.  *  subsystems developed elsewhere, which contain other copyright notices
  20.  *  in their source text.
  21.  *  
  22.  *  The Harvest software was developed by the Internet Research Task
  23.  *  Force Research Group on Resource Discovery (IRTF-RD).  The Harvest
  24.  *  software may be used for academic, research, government, and internal
  25.  *  business purposes without charge.  If you wish to sell or distribute
  26.  *  the Harvest software to commercial clients or partners, you must
  27.  *  license the software.  See
  28.  *  http://harvest.cs.colorado.edu/harvest/copyright,licensing.html#licensing.
  29.  *  
  30.  *  The Harvest software is provided ``as is'', without express or
  31.  *  implied warranty, and with no support nor obligation to assist in its
  32.  *  use, correction, modification or enhancement.  We assume no liability
  33.  *  with respect to the infringement of copyrights, trade secrets, or any
  34.  *  patents, and are not responsible for consequential damages.  Proper
  35.  *  use of the Harvest software is entirely the responsibility of the user.
  36.  *  
  37.  *  For those who are using Harvest for non-commercial purposes, you may
  38.  *  make derivative works, subject to the following constraints:
  39.  *  
  40.  *  - You must include the above copyright notice and these accompanying 
  41.  *    paragraphs in all forms of derivative works, and any documentation 
  42.  *    and other materials related to such distribution and use acknowledge 
  43.  *    that the software was developed at the above institutions.
  44.  *  
  45.  *  - You must notify IRTF-RD regarding your distribution of the 
  46.  *    derivative work.
  47.  *  
  48.  *  - You must clearly notify users that your are distributing a modified 
  49.  *    version and not the original Harvest software.
  50.  *  
  51.  *  - Any derivative product is also subject to the restrictions of the 
  52.  *    copyright, including distribution and use limitations.
  53.  */
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include "util.h"
  57. #include "url.h"
  58. #include "template.h"
  59.  
  60. static void usage()
  61. {
  62.     fprintf(stderr, "Usage: iafa2soif url local-file\n");
  63.     exit(1);
  64. }
  65.  
  66. #define isiafa(c)    (isalnum(c) || ((c) == '-') || ((c) == '#'))
  67.  
  68. static void do_iafatosoif(url, filename)
  69. char *url;
  70. char *filename;
  71. {
  72.     char buf[BUFSIZ], *s, *p;
  73.     char attr[BUFSIZ];
  74.     char value[BUFSIZ];
  75.     int i;
  76.     Template *t;
  77.     FILE *fp;
  78.     URL *up;
  79.  
  80.     if ((up = url_open(url)) == NULL) {
  81.         errorlog("Cannot open URL: %s\n", url);
  82.         return;
  83.     }
  84.  
  85.     /* Build the template */
  86.     t = create_template(NULL, up->url);
  87.  
  88.     /* Read the file and build a SOIF template from it */
  89.     if ((fp = fopen(filename, "r")) == NULL) {
  90.         log_errno(filename);
  91.         url_close(up);
  92.         return;
  93.     }
  94.     attr[0] = '\0';
  95.     while (fgets(buf, BUFSIZ, fp)) {
  96.         if ((s = strrchr(buf, '\n')) != NULL)
  97.             *s = '\0';
  98.         if (strlen(buf) < 1)
  99.             continue;
  100.         if ((s = strchr(buf, ':')) == NULL) {
  101.             append_AVList(t->list, attr, buf, strlen(buf));
  102.             continue;
  103.         }
  104.         for (p = buf, i = 0; p < s && isiafa(*p); p++, i++)
  105.             attr[i] = *p;
  106.         attr[i] = '\0';
  107.         if (i < 1)
  108.             continue;    /* null attribute */
  109.         while (*s != '\0' && (*s == ':' || isspace(*s)))
  110.             s++;
  111.         if (strlen(s) < 1)    /* empty line */
  112.             continue;
  113.         strcpy(value, s);
  114.         if (t->list)
  115.             append_AVList(t->list, attr, value, strlen(value));
  116.         else
  117.             t->list = create_AVList(attr, value, strlen(value));
  118.         
  119.     }
  120.     fclose(fp);
  121.  
  122.     /* Print out the template */
  123.     (void) init_print_template(stdout);
  124.     print_template(t);
  125.     finish_print_template();
  126.     free_template(t);
  127.     url_close(up);
  128.     return;
  129. }
  130.  
  131. int main(argc, argv)
  132. int argc;
  133. char *argv[];
  134. {
  135.     char *url, *filename;
  136.  
  137.     if (argc != 3)
  138.         usage();
  139.     url = strdup(argv[1]);
  140.     filename = strdup(argv[2]);
  141.  
  142.     init_log(stderr, stderr);
  143.     init_url();
  144.     do_iafatosoif(url, filename);
  145.     finish_url();
  146.     exit(0);
  147. }
  148.